// pages/whoAreYou/whoAreYou.jsf
let common_js = require("../../utils/common.js");
let url_js = require("../../utils/url.js");
let user_js = require("../../utils/user.js");
let sys_js = require("../../utils/system.js");
let util_js = require("../../utils/util.js");
let const_js = require("../../utils/const.js");
let ubt_js = require("../../utils/ubt.js");
let name_js = require("../../utils/ubtButtonName.js");
let api = require("../../utils/apiRequest.js");
let notificationCenter = require('../../thridLib/WxNotificationCenter.js');

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    contentStatus: -1, //1有数据
    topImgUrl: url_js.imgUrl + "whoareyou_top.png",
    leftBottomUrl: url_js.imgUrl + "whoareyou_left_bottom.png",
    rightBottomUrl: url_js.imgUrl + "whoareyou_right_bottom.png",
    selectIndex: -1,
    canCusNav: true,
    header: {
      title: '好想你心选',
      headerbg: 'linear-gradient(#C7000B,#C74E51);',
      hiddenBack: true
    },
    locationObj: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);
    //1.禁止转发
    wx.hideShareMenu();
    //2.注册通知
    this.registNotification();
    //3.初始化数据
    this.initData(options);
  },

  //初始化数据
  initData: function(options) {
    //2.请求code
    if (!user_js.getCode()) {
      api.getAccessToken();
    } else {
      this.setData({
        contentStatus: 1
      });
    }

    this.setData({
      canCusNav: app.globalData.canCusNav
    });
  },

  //注册通知，当自提点切换的时候，更新数据
  registNotification: function() {
    let that = this;
    notificationCenter.addNotification(const_js.getCodeSucessNoti, that.getCodeSuccess, that);
  },

  //微信获取code成功
  getCodeSuccess: function() {
    this.setData({
      contentStatus: 1
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    //移除通知
    let that = this;
    notificationCenter.removeNotification(const_js.getCodeSucessNoti, that);
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      selectIndex: -1
    });
  },

  //购物
  onGw: function(e) {
    this.setData({
      selectIndex: 0
    });

    //1.获取设置
    common_js.showLoading();
    wx.getSetting({
      complete: res => {
        common_js.hideLoading();
        if (common_js.containsKeyString(res.errMsg, "ok")) {
          if (!res.authSetting['scope.userLocation']) {
            //2.提前授权
            common_js.showLoading();
            wx.authorize({
              scope: 'scope.userLocation',
              complete: res => {
                common_js.hideLoading();
                if (common_js.containsKeyString(res.errMsg, "ok")) {
                  //3.获取经纬度
                  common_js.showLoading();
                  wx.getLocation({
                    complete: res => {
                      common_js.hideLoading();
                      if (common_js.containsKeyString(res.errMsg, "ok")) {
                        this.openWechatLocation(res);
                      } else {
                        common_js.showToast("获取当前位置失败，请重试!")
                      }
                    }
                  });
                } else {
                  //拒绝授权
                  //1.设置参数
                  let ztdObj = {};
                  ztdObj.autherStatus = 0;
                  wx.navigateTo({
                    url: '../selectZtPosition/selectZtPosition?ztdOjb=' + common_js.jsonStringify(ztdObj)
                  });
                }
              },
            })
          } else {
            //3.获取经纬度
            common_js.showLoading();
            wx.getLocation({
              complete: res => {
                common_js.hideLoading();
                if (common_js.containsKeyString(res.errMsg, "ok")) {
                  this.openWechatLocation(res);
                } else {
                  common_js.showToast("获取当前位置失败，请重试!")
                }
              }
            });
          }
        } else {
          common_js.showToast("获取当前设置失败，请重试!");
        }
      }
    })

    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.whoAreYouGw);
  },

  //打开微信内置地图
  openWechatLocation: function(res, noSyncLocationData) {
    const latitude = res.latitude;
    const longitude = res.longitude;
    //1.设置经纬度对象
    if (!noSyncLocationData) {
      let tempLocationObj = this.data.locationObj;
      tempLocationObj.latitude = latitude;
      tempLocationObj.longitude = longitude;
      this.setData({
        locationObj: tempLocationObj
      });
    }
    //2.打开地图选择位置
    wx.chooseLocation({
      latitude: latitude,
      longitude: longitude,
      success: res => {
        //1.设置参数
        let ztdObj = {};
        ztdObj.autherStatus = 1;
        ztdObj.name = res.name;
        ztdObj.address = res.address;
        ztdObj.latitude = res.latitude;
        ztdObj.longitude = res.longitude;
        //2.跳转
        wx.navigateTo({
          url: '../selectZtPosition/selectZtPosition?ztdOjb=' + common_js.jsonStringify(ztdObj)
        });
      },
      fail: res => {
        let canceled = res.errMsg.indexOf("cancel") != -1;
        if (canceled) {
          common_js.showModal("您未选择地理位置，我们无法为您推荐好想你心选商品。", null, null, null, null, () => {
            this.openWechatLocation(this.data.locationObj);
          });
        }
      }
    });
  },

  //新购物---新版提交
  onNewGw: function(e) {
    //1.set data
    this.setData({
      selectIndex: 0
    });

    let ztdObj = {};
    ztdObj.autherStatus = 1;
    ztdObj.name = '';
    ztdObj.address = '';
    ztdObj.latitude = null;
    ztdObj.longitude = null;
    //2.跳转
    wx.navigateTo({
      url: '../selectZtPosition/selectZtPosition?ztdOjb=' + common_js.jsonStringify(ztdObj)
    });

    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.whoAreYouGw);
  },

  //团长
  onTz: function(e) {
    this.setData({
      selectIndex: 1
    });

    wx.navigateTo({
      url: '../tobeTzFirstStep/tobeTzFirstStep'
    });

    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.whoAreYouTz);
  },

  //好货
  onHh: function(e) {
    this.setData({
      selectIndex: 2
    });

    wx.navigateTo({
      url: '../tobeSupplier/tobeSupplier'
    });

    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.whoAreYouGys);
  }
})